package com.cal.utms.dao.maintenance;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.Dealer;
import com.cal.utms.entity.DelevaryMethod;
import com.cal.utms.entity.SettlementMethod;
import com.cal.utms.logAudit.AuditLogInterceptor;
import com.cal.utms.util.HibernateUtil;

public class MasterDataDaoImpl {

	@SuppressWarnings("unchecked")
	public List<SettlementMethod> getSettlementMethodes() {

		List<SettlementMethod> methods = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			methods = session.createQuery("From SettlementMethod").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return methods;
	}

	@SuppressWarnings("unchecked")
	public List<DelevaryMethod> getDelevaryMethods() {
		List<DelevaryMethod> methods = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			methods = session.createQuery("From DelevaryMethod").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return methods;
	}

	@SuppressWarnings("unchecked")
	public List<Dealer> getDealers() {
		List<Dealer> dealers = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			dealers = session.createQuery("From Dealer").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return dealers;
	}

	public SettlementMethod getSettlementById(int settlementCode) {
		SettlementMethod method = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			method = (SettlementMethod) session.get(SettlementMethod.class, settlementCode);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return method;
	}

	public DelevaryMethod getDelevaryMethodById(int delevarymethode) {
		DelevaryMethod method = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			method = (DelevaryMethod) session.get(DelevaryMethod.class, delevarymethode);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return method;
	}

	public Dealer getdealerByid(int dealerCode) {
		Dealer dealer = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			dealer = (Dealer) session.get(Dealer.class, dealerCode);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return dealer;
	}

	public Object saveSettlement(List<SettlementMethod> methods) {
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		Object object = null;
		try {
			tx = session.beginTransaction();
			for (SettlementMethod settlementMethod : methods) {
				session.merge(settlementMethod);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return object;
	}

	public Object saveDeliveryMethod(List<DelevaryMethod> methods) {
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		Object object = null;
		try {
			tx = session.beginTransaction();
			for (DelevaryMethod delevaryMethod : methods) {
				session.merge(delevaryMethod);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return object;
	}

	public Object saveDealers(List<Dealer> methods) {
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		Object object = null;
		try {
			tx = session.beginTransaction();
			for (Dealer dealer : methods) {
				session.merge(dealer);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return object;
	}

}
